package business.inhospitalqc;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.esotericsoftware.yamlbeans.YamlException;
import common.Data;
import common.RunCaseJson;
import common.Yaml;
import io.restassured.RestAssured;
import io.restassured.response.Response;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.log4j.Logger;
import utils.GetFileMess;
import utils.YamlReadUtil;

import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;

import static common.Login.login;
import static common.TestStep.requestAndRespondBody;


public class BatchGetPatientList {
    Logger log = Logger.getLogger(BatchGetPatientList.class);
    public static final String system = "env.properties";
    Data data = login();
//    BatchCreate batchCreate=new BatchCreate();
    QcBatchList qcBatchList = new QcBatchList();
    /**
     * @param i  api-sourcedata/qc/batch/patient/list.json的datas
//     * @param j api-sourcedata/qc/batch/batchCreate.json的datas
//     * @param k api-sourcedata/dmp/template/result/list的datas
//     * @param l api-sourcedata/dmp/template/create的datas
     * @return java.util.HashMap<java.lang.String, java.lang.String>
     * @Description: 批次获取患者列表
     * @author gujinle
     * @date 2021-05-06 18:01
     */
    public String   BatchGetPatientList(int i,String batchId) throws FileNotFoundException, ConfigurationException, YamlException {
//        String batchId =batchCreate.BatchCreate(j,k,l);
        String path=new GetFileMess().getValue("ymlPath",system);
        Yaml yaml= YamlReadUtil.getLocatorMap(path,this.getClass().getName());
        JSONObject json = new JSONObject();
        json.put("access_token", data.getAccessToken());
        json.put("authToken", data.getAccessToken());
        json.put("uniqu_no", data.getUniquNo());
        json.put("operatorId", data.getUserId());
        json.put("authUserId", data.getUserId());
//        json.put("types", yaml.getDatas().get(0).get("types"));    //图表类型：1=copd，2=aecopd，3=指标检测图
        json.put("taskFlag", yaml.getDatas().get(i).get("taskFlag"));  //指派标志：0=未指派，1=已指派
        json.put("makeFlag", yaml.getDatas().get(i).get("makeFlag"));   //填写标志：0=未填写，1=已填
        json.put("checkFlag", yaml.getDatas().get(i).get("checkFlag"));   //审核标志：0=未审核，1=已审核
        json.put("batchId", batchId);     //批次ID
        json.put("page",yaml.getDatas().get(i).get("page"));
        json.put("size", yaml.getDatas().get(i).get("size"));
        log.info("请求参数：" + json.toString());
        Response response = RunCaseJson.runCase(json, "get", yaml.getUri());
        String jsonStr = response.asString();
        String url = RestAssured.baseURI + ":" + RestAssured.port + RestAssured.basePath +  yaml.getUri();

        requestAndRespondBody(url,json,jsonStr);   //写进allure报告
        log.info("响应报文：" + jsonStr);

        JSONObject jsonObject = JSON.parseObject(jsonStr);
        JSONObject obj=jsonObject.getJSONObject("responseData");
        JSONArray jsonArray = obj.getJSONArray("content");
        JSONObject content = jsonArray.getJSONObject(0);
        String ids = content.getString("ID");   //返回ids
//        HashMap hashMap=new HashMap();
//        hashMap.put("batchId",batchId);
//        hashMap.put("ids",ids);
        return ids;
    }


    public ArrayList BatchGetPatientList(int i,int j) throws FileNotFoundException, ConfigurationException, YamlException {
        JSONArray js=qcBatchList.Getbatchlist(j);
        JSONObject jb=js.getJSONObject(0);
        String batchId=jb.getString("ID");
        ArrayList arrayList=new ArrayList();
        String path=new GetFileMess().getValue("ymlPath",system);
        Yaml yaml= YamlReadUtil.getLocatorMap(path,this.getClass().getName());
        JSONObject json = new JSONObject();
        json.put("access_token", data.getAccessToken());
        json.put("authToken", data.getAccessToken());
        json.put("uniqu_no", data.getUniquNo());
        json.put("operatorId", data.getUserId());
        json.put("authUserId", data.getUserId());
        json.put("batchId", batchId);     //批次ID
        json.put("page",yaml.getDatas().get(i).get("page"));
        json.put("size", yaml.getDatas().get(i).get("size"));
        log.info("请求参数：" + json.toString());
        Response response = RunCaseJson.runCase(json, "get", yaml.getUri());
        String jsonStr = response.asString();

        String url = RestAssured.baseURI + ":" + RestAssured.port + RestAssured.basePath +  yaml.getUri();
        requestAndRespondBody(url,json,jsonStr);   //写进allure报告

        log.info("响应报文：" + jsonStr);
        JSONObject jsonObject = JSON.parseObject(jsonStr);
        JSONObject obj=jsonObject.getJSONObject("responseData");
        JSONArray contentArr = obj.getJSONArray("content");
        for (int index = 0; index < contentArr.size(); index++){
            JSONObject content = contentArr.getJSONObject(index);
            String inpatient_no = content.getString("INPATIENT_NO");
            arrayList.add(inpatient_no);
        }

        return arrayList;
    }



    public static void main(String[] args) throws FileNotFoundException, ConfigurationException, YamlException {
        BatchGetPatientList BP=new BatchGetPatientList();
        ArrayList al=BP.BatchGetPatientList(0,0);
        System.out.println(al.toArray());
    }
}
